package com.gxa.web_admin.controller.zls;

import com.gxa.common.vo.R2;
import com.gxa.modules.common_log.MyLog;
import com.gxa.modules.service.zls.ShopManageService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zls
 * Date: 2021/12/28 11:13:14
 * Description:
 * Version: V1.0
 */
@Controller
@RequestMapping("/shopManage")
public class ShopManageController {

    @Autowired(required = false)
    private ShopManageService shopManageService;

    //商铺相关信息查询
    @MyLog(value = "店铺列表查询")
    @PostMapping(value = "/queryShop2")
    @ResponseBody
    public R2 getShopList(@RequestBody Map map){
        R2 r = shopManageService.findShopList(map);
        return r;
    }

    //店铺详情查询查询
    @MyLog(value = "店铺详情查询查询")
    @PostMapping(value = "/updateShop1")
    @ResponseBody
    public R2 updateShop1(String shop_id){
        return shopManageService.queryShop1(shop_id);
    }

    //店铺注册
    @MyLog(value = "店铺注册")
    @PostMapping(value = "/addShop1")
    @ResponseBody
    public R2 addShop1(@RequestBody Map map){
        System.out.println(map);
        return shopManageService.addShop1(map);
    }

    //店铺审核
    @MyLog(value = "店铺审核")
    @PostMapping(value = "/checkShop1")
    @ResponseBody
    public R2 checkShop1(@RequestBody Map map){
        return shopManageService.checkShop1(map);
    }

    //店铺试用设置
    @MyLog(value = "店铺试用设置")
    @PostMapping(value = "/updateTryShop")
    @ResponseBody
    public R2 updateTryShop(@RequestBody Map map){
        return shopManageService.updateTryShop(map);
    }

    //图片显示测试
    @GetMapping("/getPic/{pic_name}")
    public ResponseEntity<byte[]> getImg(@PathVariable("pic_name") String pic_name) throws IOException {
//        File directory = new File("src/main/resources/static");
//        String paths = directory.getCanonicalPath();
//        String substring = paths.substring(0, 6);
//        String file = substring + File.separator + "imagesUpload";
        //通过自己写的http工具类获取到图片输入流
        InputStream inputStream = new FileInputStream(new File("D:/javaTest/" + File.separator + pic_name));
        //将输入流转为byte[]
        byte[] bytesByStream = getBytesByStream(inputStream);
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

        return new ResponseEntity<>(bytesByStream,headers, HttpStatus.OK);
    }
    public byte[]  getBytesByStream(InputStream inputStream){
        byte[] bytes = new byte[1024];

        int b;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            while((b = inputStream.read(bytes)) != -1){

                byteArrayOutputStream.write(bytes,0,b);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
